<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
<script>
  var BaseDBOperation = {
    getDBObj(dbName = "test") {
      var myPrivateData = JSON.parse(window.localStorage.getItem("myPrivateData"))
      if (!myPrivateData) {
        window.localStorage.setItem("myPrivateData", JSON.stringify({}));
      }
      myPrivateData = JSON.parse(window.localStorage.getItem("myPrivateData"))
      if (!myPrivateData[dbName]) {
        window.localStorage.setItem("myPrivateData", JSON.stringify({...myPrivateData, [dbName]: []}));
      }
      myPrivateData = JSON.parse(window.localStorage.getItem("myPrivateData"))
      // return myPrivateData[dbName]
      return myPrivateData
    },
    insert(dbName = "test", data = {}) {
      if (dbName) {
        if (!this.isEmptyObject(data)) {
          if (!data[dbName + "Id"] && data[dbName + "Id"] != '') {
            data[dbName + "Id"] = dbName + "Id" + new Date().getTime();
          }
          var dbobj = this.getDBObj(dbName)
          dbobj[dbName].push(data)
          window.localStorage.setItem("myPrivateData", JSON.stringify(dbobj));
        }

      } else {
        console.log("insert请传递数据库姓名");
      }
    },
    update(dbName = "test", filter = {}, data = {}) {
      if (dbName) {
        var dbobj = this.getDBObj(dbName)
        if (!this.isEmptyObject(data)) {
          if (this.isEmptyObject(filter)) {
            for (let i = 0; i < dbobj[dbName].length; i++) {
              dbobj[dbName][i] = data;
            }
          } else {
            for (let i = 0; i < dbobj[dbName].length; i++) {
              var order = false
              for (const key in filter) {
                if (dbobj[dbName][i][key] && dbobj[dbName][i][key] == filter[key]) {
                  order = true
                }
              }
              if (order) {
                for (const key in data) {
                  dbobj[dbName][i][key] = data[key];
                }
              }
            }
          }
        }
        window.localStorage.setItem("myPrivateData", JSON.stringify(dbobj));
        console.log("update数据", dbName, filter, data, this.select());
      } else {
        console.log("update请传递数据库姓名");
      }
    },
    delete(dbName = "test", filter = {}) {
      if (dbName) {
        var dbobj = this.getDBObj(dbName)
        if (this.isEmptyObject(filter)) {
          dbobj[dbName] = []
        } else {
          for (const key in filter) {
            dbobj[dbName] = dbobj[dbName].filter(v => v[key] != filter[key])
          }
          console.log(dbobj[dbName]);

        }
        window.localStorage.setItem("myPrivateData", JSON.stringify(dbobj));
        console.log("delete数据", dbName, filter, this.select());
      } else {
        console.log("delete请传递数据库姓名");
      }
    },
    select(dbName = "test", filter = {}) {
      if (dbName) {
        var returnDate = []
        var dbobj = this.getDBObj(dbName)
        if (this.isEmptyObject(filter)) {
          returnDate = dbobj[dbName]
        } else {
          for (const key in filter) {
            returnDate = dbobj[dbName].filter(v => v[key] == filter[key])
          }
        }
        return returnDate
        // console.log("获取数据", dbName, filter, returnDate);
      } else {
        console.log("select请传递数据库姓名");
      }
    },
    isEmptyObject(obj) {
      return JSON.stringify(obj) === '{}';
    }
  }


  // BaseDBOperation.insert("userPhone", {
  //   "telTitle": "腾讯文档",
  //   "telValue": "183637",
  //   "telTag": ["001", "002"]
  // });
  // BaseDBOperation.insert("userTag", {
  //   "tagTitle": "微信",
  //   "tagValue": "微信注册的账户"
  // });
  // BaseDBOperation.insert("userPsd", {
  //   "passTitle": "腾讯文档",
  //   "passValue": "https://docs.qq.com/sheet/DV1B1bkhVdGNvWVBT?no_promotion=1&tab=BB08J2",
  //   "passAccount": "1234",
  //   "passPassword": "1234",
  //   "passTag": ["001", "002"]
  // });


  // let pass = new Pass("", "passTitle", "passValue", "passAccount", "passPassword", "passTag");
  // let pass = new Pass("passId0.2561184557432352", "passTitle1", "passValue1", "passAccount1", "passPassword1", "passTag1");
  // pass.insert()
  // BaseDBOperation.insert("test", pass)
  // BaseDBOperation.update("test", {}, pass)
  // BaseDBOperation.delete("test", {})
  // console.log(  BaseDBOperation.select());

</script>
</body>

</html>
